package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.MvcResult;
import com.stylefeng.guns.common.persistence.model.FulltimeTrainRule;
import com.stylefeng.guns.common.persistence.model.NetworkTrainRule;
import com.stylefeng.guns.common.persistence.model.QualityGradeRule;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.generator.engine.config.ContextConfig;
import com.stylefeng.guns.modular.system.service.IDictService;
import com.stylefeng.guns.modular.system.service.IFulltimeTrainService;
import com.stylefeng.guns.modular.system.service.INetworkTrainRuleService;
import com.stylefeng.guns.modular.system.service.IQualityGradeRuleService;
import com.stylefeng.guns.modular.system.transfer.*;
import com.stylefeng.guns.modular.train.service.IEmployeeService;
import org.apache.ibatis.io.Resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jondai on 2017/12/26.
 * 积分标准控制层
 */
@Controller
@RequestMapping("/integral")
public class IntegralController extends BaseController {

    private String PREFIX = "/system/integral/";

    @Autowired
    private INetworkTrainRuleService networkTrainService;

    @Autowired
    private IFulltimeTrainService fulltimeTrainService;

    @Autowired
    private IQualityGradeRuleService qualityGradeRuleService;

    @Autowired
    private IDictService dictService;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 跳转到积分标准管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "integral.html";
//        return "/analysis/demo/test2.html";
    }

    @RequestMapping("download")
    public ResponseEntity<byte[]> download(String fileName) throws IOException {
//        Resources.setCharset(Charset.forName("UTF-8"));
//        File file = Resources.getResourceAsFile("excel/" + fileName);
        URL url = getClass().getClassLoader().getResource("excel/" + fileName);
        String path = url.getPath();
        path = java.net.URLDecoder.decode(path,"utf-8");
        System.out.println(path);
//        String path=  IntegralController.class.getClassLoader().getResource("").getPath()+"excel/";
        return super.renderFile(fileName,path);
    }


    @RequestMapping("/cleanData")
    public Object cleanData(String table) {
        employeeService.cleanData(table);
        return SUCCESS_TIP;
    }

    /**
     * 跳转到网络培训积分页面
     * @return
     */
    @RequestMapping(value = "network_train")
    public String networkTrain(Model model){
        List<NetworkTrainRule> networkTrainRules = networkTrainService.selectAll();
        Double upperLimit = 0.0;
        if(networkTrainRules.size() > 0){
            upperLimit = networkTrainRules.get(0).getUpperLimit();
        }

        model.addAttribute("networkTrainRules", networkTrainRules);
        model.addAttribute("upperLimit", upperLimit);
        return PREFIX + "network_train.html";
    }

    /**
     * 跳转到脱产培训积分页面
     * @return
     */
    @RequestMapping(value = "fulltime_train")
    public String fullTimeTrain(Model model){

        List<FulltimeTrainRule> fulltimeTrainRules = fulltimeTrainService.selectAll();

        List<FulltimeTrainRule> jyTrains = new ArrayList<>();
        List<FulltimeTrainRule> glTrains = new ArrayList<>();
        List<FulltimeTrainRule> jsTrains = new ArrayList<>();
        List<FulltimeTrainRule> jnTrains = new ArrayList<>();

        Integer jyHourLessThan = 0;
        Integer glHourLessThan = 0;
        Integer jsHourLessThan = 0;
        Integer jnHourLessThan = 0;

        fulltimeTrainRules.forEach(e -> {
            switch (e.getEmpType()){
                case Const.JINGYING_RENYUAN : jyTrains.add(e); break;
                case Const.GUANLI_RENYUAN : glTrains.add(e); break;
                case Const.JISHU_RENYUAN : jsTrains.add(e); break;
                case Const.JINENG_RENYUAN : jnTrains.add(e); break;
            }
        });

        model.addAttribute("jyHourLessThan", jyTrains.size() > 0 ? jyTrains.get(0).getLeastHour() : jyHourLessThan);
        model.addAttribute("glHourLessThan", glTrains.size() > 0 ? glTrains.get(0).getLeastHour() : glHourLessThan);
        model.addAttribute("jsHourLessThan", jsTrains.size() > 0 ? jsTrains.get(0).getLeastHour() : jsHourLessThan);
        model.addAttribute("jnHourLessThan", jnTrains.size() > 0 ? jnTrains.get(0).getLeastHour() : jnHourLessThan);

        model.addAttribute("jyTrains",jyTrains);
        model.addAttribute("glTrains",glTrains);
        model.addAttribute("jsTrains",jsTrains);
        model.addAttribute("jnTrains",jnTrains);


        return PREFIX + "fulltime_train.html";
    }

    /**
     * 跳转到学历提升积分页面
     * @return
     */
    @RequestMapping(value = "degree_train")
    public String degreeTrain(Model model){
        Map<String,Object> condition = new HashMap<>();
        condition.put(QualityGradeRule.gradeTypeName,Const.DEGREE_TRAIN);
        List<QualityGradeRule> qualityGradeRules = qualityGradeRuleService.selectByMap(condition);

        model.addAttribute("degreeTrainRules",qualityGradeRules);
        return PREFIX + "degree_train.html";
    }

    /**
     * 跳转到专业技术资格提升积分页面
     * @return
     */
    @RequestMapping(value = "pro_tech_train")
    public String proTechTrain(Model model){
        Map<String,Object> condition = new HashMap<>();
        condition.put(QualityGradeRule.gradeTypeName,Const.PRO_TECH_TRAIN);
        List<QualityGradeRule> qualityGradeRules = qualityGradeRuleService.selectByMap(condition);

        model.addAttribute("proTechTrainRules",qualityGradeRules);
        return PREFIX + "pro_tech_train.html";
    }


    /**
     * 跳转到执业资格提升积分页面
     * @return
     */
    @RequestMapping(value = "train_start")
    public String trainStart(Model model){
        QualityGradeRule qualityGradeRule = new QualityGradeRule();
        qualityGradeRule.setGradeType(Const.TRAIN_START);
        EntityWrapper<QualityGradeRule> wrapper = new EntityWrapper<>();
        wrapper.setEntity(qualityGradeRule);
        wrapper.orderBy("integral");
        List<QualityGradeRule> qualityGradeRules = qualityGradeRuleService.selectList(wrapper);

//        Map<String,Object> condition = new HashMap<>();
//        condition.put(QualityGradeRule.gradeTypeName,Const.TRAIN_START);
//
//        List<QualityGradeRule> qualityGradeRules = qualityGradeRuleService.selectByMap(condition);
        int count = 0;
        if(qualityGradeRules == null || qualityGradeRules.size() < 1){

        }else{
            count = qualityGradeRules.size();
        }
        for (int i=0; i<5-count; i++){
            qualityGradeRules.add(new QualityGradeRule());
        }
//        if(qualityGradeRules == null || qualityGradeRules.size() < 1){
//            qualityGradeRules = new ArrayList<>();
//            qualityGradeRules.add(new QualityGradeRule());
//            qualityGradeRules.add(new QualityGradeRule());
//            qualityGradeRules.add(new QualityGradeRule());
//            qualityGradeRules.add(new QualityGradeRule());
//            qualityGradeRules.add(new QualityGradeRule());
//        }
        model.addAttribute("startRules",qualityGradeRules);
        return PREFIX + "train_start.html";
    }

    /**
     * 跳转到执业资格提升积分页面
     * @return
     */
    @RequestMapping(value = "practice_train")
    public String practiceTrain(Model model){
        Map<String,Object> condition = new HashMap<>();
        condition.put(QualityGradeRule.gradeTypeName,Const.PRACTICE_TRAIN);
        List<QualityGradeRule> qualityGradeRules = qualityGradeRuleService.selectByMap(condition);

        model.addAttribute("practiceRules",qualityGradeRules);
        return PREFIX + "practicePoints.html";
    }

    /**
     * 跳转到职业资格提升积分页面
     * @return
     */
    @RequestMapping(value = "pro_qualify_train")
    public String proQualifyTrain(Model model){
        Map<String,Object> condition = new HashMap<>();
        condition.put(QualityGradeRule.gradeTypeName,Const.PRO_QUALIFY_TRAIN);
        List<QualityGradeRule> qualityGradeRules = qualityGradeRuleService.selectByMap(condition);

        model.addAttribute("proQualifyTrainRules",qualityGradeRules);
        return PREFIX + "pro_qualify_train.html";
    }

    /**
     * 跳转到培训项目开发积分页面
     * @return
     */
    @RequestMapping(value = "master")
    public String master(Model model){
        Map<String,Object> condition = new HashMap<>();
        condition.put(QualityGradeRule.gradeTypeName,Const.TEACHER);
        List<QualityGradeRule> teacherRule = qualityGradeRuleService.selectByMap(condition);
        if(teacherRule != null && teacherRule.size() > 0){
            model.addAttribute("teacherRule",teacherRule.get(0));
        }else{
            model.addAttribute("teacherRule",new QualityGradeRule());
        }

        condition.clear();
        condition.put(QualityGradeRule.gradeTypeName,Const.MASTER);
        List<QualityGradeRule> masterRule = qualityGradeRuleService.selectByMap(condition);
        if(masterRule != null && masterRule.size() > 0){
            model.addAttribute("masterRule",masterRule.get(0));
        }else{
            model.addAttribute("masterRule",new QualityGradeRule());
        }
        return PREFIX + "master.html";
    }

    /**
     * 跳转到师徒带积分页面
     * @return
     */
    @RequestMapping(value = "train_program")
    public String trainProgram(Model model){
        Map<String,Object> condition = new HashMap<>();
        condition.put(QualityGradeRule.gradeTypeName,Const.TRAINING_PROGRAM);
        List<QualityGradeRule> qualityGradeRules = qualityGradeRuleService.selectByMap(condition);

        model.addAttribute("trainProgramRules",qualityGradeRules);
        return PREFIX + "train_program.html";
    }
    /**
     * 跳转到专家人才页面
     * @return
     */
    @RequestMapping(value = "expert_train")
    public String expertTrain(Model model){

        //获取字典中所有的专家人才
        Map<String,Object> condition = new HashMap<>();
        //国家级
        condition.put(QualityGradeRule.gradeTypeName, Const.EXPERT_COUNTRY);
        List<QualityGradeRule> country = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("countryRules",country);
        //国网公司级
        condition.clear();
        condition.put(QualityGradeRule.gradeTypeName, Const.EXPERT_STATEGRID);
        List<QualityGradeRule> stateGrid = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("stateGridRules",stateGrid);

        //省公司级
        condition.clear();
        condition.put(QualityGradeRule.gradeTypeName, Const.EXPERT_PROVINCE);
        List<QualityGradeRule> province = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("provinceRules",province);

        //地级市公司级
        condition.clear();
        condition.put(QualityGradeRule.gradeTypeName, Const.EXPERT_CITY);
        List<QualityGradeRule> city = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("cityRules",city);

        //省部（行业）级
        condition.clear();
        condition.put(QualityGradeRule.gradeTypeName, Const.PROVINCE_VICE);
        List<QualityGradeRule> provinceViceRules = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("provinceViceRules",provinceViceRules);

        //地市（厅局）级
        condition.clear();
        condition.put(QualityGradeRule.gradeTypeName, Const.CITY_VICE);
        List<QualityGradeRule> cityViceRules = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("cityViceRules",cityViceRules);

        return PREFIX + "expert_train.html";
    }


    /**
     * 跳转到兼职培训师页面
     * @return
     */
    @RequestMapping(value = "part_time_trainer")
    public String partTimeTrainer(Model model){

        //获取字典中所有的兼职培训师
        Map<String,Object> condition = new HashMap<>();
        //认证兼职培训师
        condition.put(QualityGradeRule.gradeTypeName, Const.TRAINER_LEVEL_AUTHENTICATION);
        List<QualityGradeRule> authentications = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("authenticationRules",authentications);

        //优秀兼职培训师
        condition.clear();
        condition.put(QualityGradeRule.gradeTypeName, Const.TRAINER_LEVEL_EXCELLENT);
        List<QualityGradeRule> excellents = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("excellentRules",excellents);

        //项目制兼职培训师
        condition.clear();
        condition.put(QualityGradeRule.gradeTypeName, Const.TRAINER_LEVEL_PROJECT);
        List<QualityGradeRule> projects = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("projectRules",projects);

        //周期制兼职培训师
        condition.clear();
        condition.put(QualityGradeRule.gradeTypeName, Const.TRAINER_LEVEL_CYCLE);
        List<QualityGradeRule> cycles = qualityGradeRuleService.selectByMap(condition);
        model.addAttribute("cycleRules",cycles);

        return PREFIX + "train_ability.html";
    }



    /**
     * 网络培训添加、更新
     * @param networkTrains
     * @return
     */
    @RequestMapping(value = "network_train_add", method = RequestMethod.POST)
    @ResponseBody
    public Object networkTrainAdd(NetworkTrainDto networkTrains) {
        networkTrainService.empty();
        List<NetworkTrainRule> networkTrainRuleList = new ArrayList<>();

        networkTrains.getNetworkTrainRules().forEach(e -> {
            e.setUpperLimit(networkTrains.getUpperLimit());
            if(e.getCredit() != null || e.getStartHours() != null || e.getEndHours() != null ){
                networkTrainRuleList.add(e);
            }
        });
        networkTrainService.insertBatch(networkTrainRuleList);

        return super.SUCCESS_TIP;
    }



    /**
     * 脱产培训积分添加
     * 脱产培训四种类型：经营人员、管理人员、技术人员、技能人员
     * 更新或者删除时删除表中所有记录
     * @param fullTimeTrains
     * @return
     */
    @RequestMapping(value = "fulltime_train_add", method = RequestMethod.POST)
    @ResponseBody
    public Object fulltimeTrainAdd(FullTimeTrainDto fullTimeTrains) {
        fulltimeTrainService.empty();

        List<FulltimeTrainRule> jyTrains = new ArrayList<>();
        List<FulltimeTrainRule> glTrains = new ArrayList<>();
        List<FulltimeTrainRule> jsTrains = new ArrayList<>();
        List<FulltimeTrainRule> jnTrains = new ArrayList<>();

        List<FulltimeTrainRule> jyEmpTrain = fullTimeTrains.getJyEmpTrain();
        if(jyEmpTrain != null && jyEmpTrain.size() > 0){
            jyEmpTrain.forEach(e -> {

                if(e.getCredit() != null || e.getStartHour() != null || e.getEndHour() != null ){
                    e.setLeastHour(fullTimeTrains.getJyHourLessThan());
                    e.setEmpType(Const.JINGYING_RENYUAN);
                    e.setState(Const.STATE_ACTIVATION);
                    jyTrains.add(e);
                }
            });
            fulltimeTrainService.insertBatch(jyTrains);
        }

        List<FulltimeTrainRule> glEmpTrain = fullTimeTrains.getGlEmpTrain();
        if(glEmpTrain != null && glEmpTrain.size() > 0){
            glEmpTrain.forEach(e -> {

                if(e.getCredit() != null || e.getStartHour() != null || e.getEndHour() != null ){
                    e.setLeastHour(fullTimeTrains.getGlHourLessThan());
                    e.setEmpType(Const.GUANLI_RENYUAN);
                    e.setState(Const.STATE_ACTIVATION);
                    glTrains.add(e);
                }
            });
            fulltimeTrainService.insertBatch(glTrains);
        }

        List<FulltimeTrainRule> jsEmpTrain = fullTimeTrains.getJsEmpTrain();
        if(jsEmpTrain != null && jsEmpTrain.size() > 0){
            jsEmpTrain.forEach(e -> {

                if(e.getCredit() != null || e.getStartHour() != null || e.getEndHour() != null ){
                    e.setLeastHour(fullTimeTrains.getJsHourLessThan());
                    e.setEmpType(Const.JISHU_RENYUAN);
                    e.setState(Const.STATE_ACTIVATION);
                    jsTrains.add(e);
                }
            });
            fulltimeTrainService.insertBatch(jsTrains);
        }

        List<FulltimeTrainRule> jnEmpTrain = fullTimeTrains.getJnEmpTrain();
        if(jnEmpTrain != null && jnEmpTrain.size() > 0){
            jnEmpTrain.forEach(e -> {

                if(e.getCredit() != null || e.getStartHour() != null || e.getEndHour() != null ){
                    e.setLeastHour(fullTimeTrains.getJnHourLessThan());
                    e.setEmpType(Const.JINENG_RENYUAN);
                    e.setState(Const.STATE_ACTIVATION);
                    jnTrains.add(e);
                }
            });
            fulltimeTrainService.insertBatch(jnTrains);
        }

        return super.SUCCESS_TIP;
    }


    /**
     * 更新学历提升积分
     * @param degreeTrainRules
     * @return
     */
    @RequestMapping(value = "degree_train_add", method = RequestMethod.POST)
    @ResponseBody
    public Object DegreeTrainAdd(QualityGradeRuleDto degreeTrainRules){

        //清空
        qualityGradeRuleService.emptyByGradeType(Const.DEGREE_TRAIN);

        List<QualityGradeRule> degreeRules = degreeTrainRules.getDegreeTrainRules();

        List<QualityGradeRule> qualityGradeRuleList = new ArrayList<>();

        for (QualityGradeRule degreeTrainRule : degreeRules) {
            if(!StringUtils.isEmpty(degreeTrainRule.getGrade()) && degreeTrainRule.getIntegral() != null){
                degreeTrainRule.setGradeType(Const.DEGREE_TRAIN);
                qualityGradeRuleList.add(degreeTrainRule);
            }

        }

        //检查编码是否重复
        MvcResult isRepeat = qualityGradeRuleService.checkGradeCode(qualityGradeRuleList);
        if("true".equals(isRepeat.getState())){
            return new ErrorTip(500,isRepeat.getMsg());
        }

        qualityGradeRuleService.insertBatch(qualityGradeRuleList);

        return super.SUCCESS_TIP;
    }

    /**
     * 更新专业技术资格提升积分
     * @param proTechTrainRules
     * @return
     */
    @RequestMapping(value = "pro_tech_train_add", method = RequestMethod.POST)
    @ResponseBody
    public Object proTechTrainAdd(QualityGradeRuleDto proTechTrainRules){

        //清空
        qualityGradeRuleService.emptyByGradeType(Const.PRO_TECH_TRAIN);

        List<QualityGradeRule> proTechRules = proTechTrainRules.getProTechTrainRules();

        List<QualityGradeRule> proTechTrainRuleList = new ArrayList<>();

        for (QualityGradeRule proTechRule : proTechRules) {
            if(!StringUtils.isEmpty(proTechRule.getGrade()) && proTechRule.getIntegral() != null){
                proTechRule.setGradeType(Const.PRO_TECH_TRAIN);
                proTechTrainRuleList.add(proTechRule);
            }

        }

        //检查编码是否重复
        MvcResult isRepeat = qualityGradeRuleService.checkGradeCode(proTechTrainRuleList);
        if("true".equals(isRepeat.getState())){
            return new ErrorTip(500,isRepeat.getMsg());
        }

        qualityGradeRuleService.insertBatch(proTechTrainRuleList);

        return super.SUCCESS_TIP;
    }
    /**
     * 更新职业资格提升积分
     * @param proQualifyTrainRules
     * @return
     */
    @RequestMapping(value = "pro_qualify_train_add", method = RequestMethod.POST)
    @ResponseBody
    public Object proQualifyTrainAdd(QualityGradeRuleDto proQualifyTrainRules){

        //清空
        qualityGradeRuleService.emptyByGradeType(Const.PRO_QUALIFY_TRAIN);

        List<QualityGradeRule> proQualifyRules = proQualifyTrainRules.getProQualifyTrainRules();

        List<QualityGradeRule> proQualifyRuleList = new ArrayList<>();

        for (QualityGradeRule proQualify : proQualifyRules) {
            if(!StringUtils.isEmpty(proQualify.getGrade()) && proQualify.getIntegral() != null){
                proQualify.setGradeType(Const.PRO_QUALIFY_TRAIN);
                proQualifyRuleList.add(proQualify);
            }

        }

        //检查编码是否重复
        MvcResult isRepeat = qualityGradeRuleService.checkGradeCode(proQualifyRuleList);
        if("true".equals(isRepeat.getState())){
            return new ErrorTip(500,isRepeat.getMsg());
        }

        qualityGradeRuleService.insertBatch(proQualifyRuleList);

        return super.SUCCESS_TIP;
    }

    /**
     * 更新星级积分
     * @param proQualifyTrainRules
     * @return
     */
    @RequestMapping(value = "train_start_add", method = RequestMethod.POST)
    @ResponseBody
    public Object trainStartAdd(QualityGradeRuleDto proQualifyTrainRules){

        //清空
        qualityGradeRuleService.emptyByGradeType(Const.TRAIN_START);

        List<QualityGradeRule> proQualifyRules = proQualifyTrainRules.getStartTrainRules();

        List<QualityGradeRule> proQualifyRuleList = new ArrayList<>();

        for (QualityGradeRule proQualify : proQualifyRules) {
            if(!StringUtils.isEmpty(proQualify.getGrade()) && proQualify.getIntegral() != null){
                proQualify.setGradeType(Const.TRAIN_START);
                proQualifyRuleList.add(proQualify);
            }

        }

//        //检查编码是否重复
//        MvcResult isRepeat = qualityGradeRuleService.checkGradeCode(proQualifyRuleList);
//        if("true".equals(isRepeat.getState())){
//            return new ErrorTip(500,isRepeat.getMsg());
//        }

        qualityGradeRuleService.insertBatch(proQualifyRuleList);

        return super.SUCCESS_TIP;
    }
    /**
     * 更新执业资格提升积分
     * @param practiceRules
     * @return
     */
    @RequestMapping(value = "practice_train_add", method = RequestMethod.POST)
    @ResponseBody
    public Object practiceTrainAdd(QualityGradeRuleDto practiceRules){

        //清空
        qualityGradeRuleService.emptyByGradeType(Const.PRACTICE_TRAIN);

        List<QualityGradeRule> practiceRuleList = practiceRules.getPracticeRules();

        List<QualityGradeRule> proQualifyRuleList = new ArrayList<>();

        for (QualityGradeRule proQualify : practiceRuleList) {
            if(!StringUtils.isEmpty(proQualify.getGrade()) && proQualify.getIntegral() != null){
                proQualify.setGradeType(Const.PRACTICE_TRAIN);
                proQualifyRuleList.add(proQualify);
            }

        }

        //检查编码是否重复
        MvcResult isRepeat = qualityGradeRuleService.checkGradeCode(proQualifyRuleList);
        if("true".equals(isRepeat.getState())){
            return new ErrorTip(500,isRepeat.getMsg());
        }

        qualityGradeRuleService.insertBatch(proQualifyRuleList);

        return super.SUCCESS_TIP;
    }

    /**
     * 更新培训项目开发积分
     * @param trainProgramRules
     * @return
     */
    @RequestMapping(value = "train_program_add", method = RequestMethod.POST)
    @ResponseBody
    public Object trainProgramAdd(QualityGradeRuleDto trainProgramRules){

        //清空
        qualityGradeRuleService.emptyByGradeType(Const.TRAINING_PROGRAM);

        List<QualityGradeRule> trainProgramRuless = trainProgramRules.getTrainProgramRules();

        List<QualityGradeRule> proQualifyRuleList = new ArrayList<>();

        for (QualityGradeRule proQualify : trainProgramRuless) {
            if(!StringUtils.isEmpty(proQualify.getGrade()) && proQualify.getIntegral() != null){
                proQualify.setGradeType(Const.TRAINING_PROGRAM);
                proQualifyRuleList.add(proQualify);
            }

        }

        //检查编码是否重复
        MvcResult isRepeat = qualityGradeRuleService.checkGradeCode(proQualifyRuleList);
        if("true".equals(isRepeat.getState())){
            return new ErrorTip(500,isRepeat.getMsg());
        }

        qualityGradeRuleService.insertBatch(proQualifyRuleList);

        return super.SUCCESS_TIP;
    }


    /**
     * 更新培训项目开发积分
     * @param masterDto
     * @return
     */
    @RequestMapping(value = "master_add", method = RequestMethod.POST)
    @ResponseBody
    public Object masterAdd(MasterDto masterDto){
        //清空
        qualityGradeRuleService.emptyByGradeType(Const.TEACHER);
        qualityGradeRuleService.emptyByGradeType(Const.MASTER);

        QualityGradeRule masterRule = masterDto.getMasterRule();
        masterRule.setGrade("师徒");
        masterRule.setGradeCode("1000666");
        masterRule.setGradeType(Const.MASTER);

        QualityGradeRule teacherRule = masterDto.getTeacherRule();
        teacherRule.setGradeCode("1000555");
        teacherRule.setGrade("师傅最高");
        teacherRule.setGradeType(Const.TEACHER);

        qualityGradeRuleService.insert(masterRule);
        qualityGradeRuleService.insert(teacherRule);

        return super.SUCCESS_TIP;
    }

    /**
     * 更新专家人才积分规则
     * @param expertTrainDto
     * @return
     */
    @RequestMapping(value = "expert_train_add", method = RequestMethod.POST)
    @ResponseBody
    public Object expertTrainAdd(ExpertTrainDto expertTrainDto){

        List<QualityGradeRule> allGradeRules = new ArrayList<>();
        //国家级
        qualityGradeRuleService.emptyByGradeType(Const.EXPERT_COUNTRY);
        List<QualityGradeRule> countryRule = expertTrainDto.getCountryRule();
        if(!CollectionUtils.isEmpty(countryRule)){
            for (QualityGradeRule qualityGradeRule : countryRule) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null){
                    qualityGradeRule.setGradeType(Const.EXPERT_COUNTRY);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }


        //国网公司级
        qualityGradeRuleService.emptyByGradeType(Const.EXPERT_STATEGRID);
        List<QualityGradeRule> stateGridRule = expertTrainDto.getStateGridRule();
        if(!CollectionUtils.isEmpty(stateGridRule)) {
            for (QualityGradeRule qualityGradeRule : stateGridRule) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null) {
                    qualityGradeRule.setGradeType(Const.EXPERT_STATEGRID);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }

        //省公司级
        qualityGradeRuleService.emptyByGradeType(Const.EXPERT_PROVINCE);
        List<QualityGradeRule> provinceRules = expertTrainDto.getProvinceRule();
        if(!CollectionUtils.isEmpty(provinceRules)) {
            for (QualityGradeRule qualityGradeRule : provinceRules) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null) {
                    qualityGradeRule.setGradeType(Const.EXPERT_PROVINCE);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }

        //地市级
        qualityGradeRuleService.emptyByGradeType(Const.EXPERT_CITY);
        List<QualityGradeRule> cityRules = expertTrainDto.getCityRule();
        if(!CollectionUtils.isEmpty(cityRules)) {
            for (QualityGradeRule qualityGradeRule : cityRules) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null) {
                    qualityGradeRule.setGradeType(Const.EXPERT_CITY);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }

        //省部（行业）级
        qualityGradeRuleService.emptyByGradeType(Const.PROVINCE_VICE);
        List<QualityGradeRule> provinceViceRule = expertTrainDto.getProvinceViceRule();
        if(!CollectionUtils.isEmpty(provinceViceRule)){
            for (QualityGradeRule qualityGradeRule : provinceViceRule) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null){
                    qualityGradeRule.setGradeType(Const.PROVINCE_VICE);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }

        //地市（厅局）级
        qualityGradeRuleService.emptyByGradeType(Const.CITY_VICE);
        List<QualityGradeRule> cityViceRule = expertTrainDto.getCityViceRule();
        if(!CollectionUtils.isEmpty(cityViceRule)){
            for (QualityGradeRule qualityGradeRule : cityViceRule) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null){
                    qualityGradeRule.setGradeType(Const.CITY_VICE);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }

        //检查编码是否重复
        MvcResult isRepeat = qualityGradeRuleService.checkGradeCode(allGradeRules);
        if("true".equals(isRepeat.getState())){
            return new ErrorTip(500,isRepeat.getMsg());
        }

        qualityGradeRuleService.insertBatch(allGradeRules);

        return super.SUCCESS_TIP;
    }


    /**
     * 更新兼职培训师积分规则
     * @param trainPartTimerDto
     * @return
     */
    @RequestMapping(value = "part_time_trainer_add", method = RequestMethod.POST)
    @ResponseBody
    public Object partTimeTrainerAdd(TrainPartTimerDto trainPartTimerDto){

        List<QualityGradeRule> allGradeRules = new ArrayList<>();
        //认证兼职培训师
        qualityGradeRuleService.emptyByGradeType(Const.TRAINER_LEVEL_AUTHENTICATION);
        List<QualityGradeRule> authenticationRules = trainPartTimerDto.getAuthenticationRules();
        if(!CollectionUtils.isEmpty(authenticationRules)){
            for (QualityGradeRule qualityGradeRule : authenticationRules) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null){
                    qualityGradeRule.setGradeType(Const.TRAINER_LEVEL_AUTHENTICATION);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }


        //优秀兼职培训师
        qualityGradeRuleService.emptyByGradeType(Const.TRAINER_LEVEL_EXCELLENT);
        List<QualityGradeRule> excellentRules = trainPartTimerDto.getExcellentRules();
        if(!CollectionUtils.isEmpty(excellentRules)) {
            for (QualityGradeRule qualityGradeRule : excellentRules) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null) {
                    qualityGradeRule.setGradeType(Const.TRAINER_LEVEL_EXCELLENT);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }

        //项目制兼职培训师
        qualityGradeRuleService.emptyByGradeType(Const.TRAINER_LEVEL_PROJECT);
        List<QualityGradeRule> projectRules = trainPartTimerDto.getProjectRules();
        if(!CollectionUtils.isEmpty(projectRules)) {
            for (QualityGradeRule qualityGradeRule : projectRules) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null) {
                    qualityGradeRule.setGradeType(Const.TRAINER_LEVEL_PROJECT);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }

        //地市级
        qualityGradeRuleService.emptyByGradeType(Const.TRAINER_LEVEL_CYCLE);
        List<QualityGradeRule> cycleRules = trainPartTimerDto.getCycleRules();
        if(!CollectionUtils.isEmpty(cycleRules)) {
            for (QualityGradeRule qualityGradeRule : cycleRules) {
                if(!StringUtils.isEmpty(qualityGradeRule.getGrade()) && qualityGradeRule.getIntegral() != null) {
                    qualityGradeRule.setGradeType(Const.TRAINER_LEVEL_CYCLE);
                    allGradeRules.add(qualityGradeRule);
                }
            }
        }


        //检查编码是否重复
        MvcResult isRepeat = qualityGradeRuleService.checkGradeCode(allGradeRules);
        if("true".equals(isRepeat.getState())){
            return new ErrorTip(500,isRepeat.getMsg());
        }

        qualityGradeRuleService.insertBatch(allGradeRules);

        return super.SUCCESS_TIP;
    }

}
